﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using System.Collections.ObjectModel;
using SMS.Entities;
using System.Windows.Data;
using SMS.Common.Silverlight;
using System.ComponentModel;

namespace SMS.ViewModel.Purchase
{
    public class ProductStockWiseViewModel : BaseNotify
    {
        private SMS.Model.StockMasterModel _model = null;

        public ProductStockWiseViewModel(int StockID)
        {
            NewCommand = new DelegateCommand(OnNew, null);
            SaveCommand = new DelegateCommand(OnSave, null);
            CancelCommand = new DelegateCommand(OnCancel, null);
            LoadCommand = new DelegateCommand(OnLoadCommand, null);
            if (!DesignerProperties.IsInDesignTool)
            {
                _model = new Model.StockMasterModel();
                _model.OnGetCompletedProductStockWiseCompleted += new Action<ProductStockWise>(_model_OnGetCompletedProductStockWiseCompleted);
                _model.OnGetProductsByStockCompleted += new Action<ObservableCollection<ProductStockWise>>(_model_OnGetProductsByStockCompleted);
                _model.OnProductStockWiseSaved += new Action<int>(_model_OnProductStockWiseSaved);
                _model.OnServiceError += new Action<SMSException>(_model_OnServiceError);
            }
            SelectedStockID = StockID;
            if (SelectedStockID > 0)
                _model.GetProductsByStock(App.LoggedinUserName, SelectedStockID);
            else
                UserMessage = "Enter stock ID to load products";
        }

        void _model_OnServiceError(SMSException obj)
        {
            ErrorMessage = obj.Message;
            UserMessage = string.Empty;
        }

        void _model_OnProductStockWiseSaved(int obj)
        {
            CompletedProduct.Product_Stock_ID = obj;
            UserMessage = "Product Saved Successfully";
            ErrorMessage = string.Empty;
        }

        void _model_OnGetProductsByStockCompleted(ObservableCollection<ProductStockWise> obj)
        {
            ProductList = obj;
            PagedProductList = new PagedCollectionView(obj);
            UserMessage = "Products loaded";
            ErrorMessage = string.Empty;
        }

        void _model_OnGetCompletedProductStockWiseCompleted(ProductStockWise obj)
        {
            CompletedProduct = obj;
            UserMessage = "Full Product Detail loaded";
            ErrorMessage = string.Empty;
        }

        private int _SelectedStockId;

        public int SelectedStockID
        {
            get { return _SelectedStockId; }
            set { _SelectedStockId = value; OnPropertyChanged("SelectedStockID"); }
        }

        private ObservableCollection<ProductStockWise> _ProductList;

        public ObservableCollection<ProductStockWise> ProductList
        {
            get { return _ProductList; }
            set { _ProductList = value; OnPropertyChanged("ProductList"); }
        }
        private PagedCollectionView _PagedProductList;
        public PagedCollectionView PagedProductList
        {
            get { return _PagedProductList; }
            set { _PagedProductList = value; OnPropertyChanged("PagedProductList"); }
        }

        private ProductStockWise _SelectedProduct;

        public ProductStockWise SelectedProduct
        {
            get { return _SelectedProduct; }
            set
            {
                _SelectedProduct = value;
                if (IsLoadComplete && value != null)
                {
                    if (value.Product_Stock_ID > 0)
                    {
                        //value.Discount
                        _model.GetCompleteProduct(App.LoggedinUserName, value.Product_Stock_ID);
                        UserMessage = "Getting complete Product...";
                    }
                    else
                    {
                        CompletedProduct = value;
                    }
                }
                OnPropertyChanged("SelectedProduct");
            }
        }

        private ProductStockWise _CompletedProduct;

        public ProductStockWise CompletedProduct
        {
            get { return _CompletedProduct; }
            set { _CompletedProduct = value; OnPropertyChanged("CompletedProduct"); }
        }


        private bool IsLoadComplete = true;

        public ICommand NewCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand LoadCommand { get; private set; }
        public ICommand AddProductCommand { get; private set; }

        public void OnNew(object param)
        {
            IsLoadComplete = false;
            if (ProductList == null)
                ProductList = new ObservableCollection<ProductStockWise>();
            var newObj = new ProductStockWise { UOM = "uom", Stock = new StockMaster { Stock_ID = SelectedStockID }, ProductBrand = new Brand { Brand_ID = 1 } };
            ProductList.Add(newObj);
            PagedProductList = new PagedCollectionView(ProductList);
            PagedProductList.MoveToLastPage();
            IsLoadComplete = true;
            SelectedProduct = newObj;

        }

        private void OnSave(object param)
        {
            UserMessage = string.Empty;
            if (CompletedProduct != null)
            {
                if (CompletedProduct.Product == null)
                {
                    ErrorMessage = "Please select product";
                    return;
                }
                if (CompletedProduct.Quantity <= 0)
                {
                    ErrorMessage = "Quantity should be greater than 0";
                    return;
                }
                if (CompletedProduct.BuyingPrice <= 0)
                {
                    ErrorMessage = "Buying Unit Price should be greater than 0";
                    return;
                }

                _model.SaveProductStockWise(App.LoggedinUserName, CompletedProduct);
                UserMessage = "Saving Stock Master...";
            }
            else
            {
                ErrorMessage = "No Product to save";
                UserMessage = string.Empty;
            }
        }
        private void OnCancel(object param)
        {

        }
        public void OnLoadCommand(object param)
        {
            _model.GetProductsByStock(App.LoggedinUserName, SelectedStockID);
            UserMessage = "Loading Stock Master, Please wait...";
        }

    }
}
